<?php
/**
 * Created by PhpStorm.
 * User: hongyeah
 * Date: 2017/9/18
 * Time: 22:19
 */

/**
 * 0 成功
 * -1 系统故障(取不到数据或逻辑有误)
 * -3 参数有误
 * -5 业务操作失败,必须带msg
 * -999 没有登录
 * -998 账号不存在
 * */
define('CODE_OK', 0);
define('CODE_SYSTEM_FAIL', -1);
define('CODE_PARAM_FAIL', -3);
define('CODE_OPT_FAIL', -5);
define('CODE_NO_LOGIN', -999);
define('CODE_NOT_EXIST', -998);
define('CODE_EXIST', -997);

/**
 * 发送手机验证码
 * */
function sendPhoneCode($phone, $code)
{
    // 以下为核心代码部分
    $ch = curl_init();
    // 必要参数
    $apikey = "cb20310db0e7dcc76a827afbd2ba548f"; //修改为您的apikey(https://www.yunpian.com)登录官网后获取
    $mobile = $phone; //请用手机号代替
    $text="【抵奥云科技】您的验证码是{$code}";
    // 发送短信
    $data=array('text'=>$text,'apikey'=>$apikey,'mobile'=>$mobile);
    curl_setopt ($ch, CURLOPT_URL, 'https://sms.yunpian.com/v2/sms/single_send.json');
    curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
    curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);
    $json_data = curl_exec($ch);
    //如果curl发生错误，打印出错误
    if(curl_error($ch) != ""){
        echo 'Curl error: ' .curl_error($ch);
    }
    //解析返回结果（json格式字符串）
    $array = json_decode($json_data,true);
    return $array;
}

function myRedis()
{
    return \think\Cache::store('redis');
}

function makeSkey($str)
{
    return md5(base64_encode(md5($str)));
}

function qiNiuToken()
{
    $accessKey = \think\Config::get('qiniu.access_key');
    $secretKey = \think\Config::get('qiniu.secret_key');
    $auth = new \Qiniu\Auth($accessKey, $secretKey);
    $token = $auth->uploadToken(\think\Config::get('qiniu.bucket'));
    return $token;
}


/**
 * 根据卡号获取银行名称
 * */
function getBankName($card)
{
    $bankList = \think\Config::get('banklist');
    $card_8 = substr($card, 0, 8);
    if (isset($bankList[$card_8])) {
        return $bankList[$card_8];
    }
    $card_6 = substr($card, 0, 6);
    if (isset($bankList[$card_6])) {
        return  $bankList[$card_6];
    }
    $card_5 = substr($card, 0, 5);
    if (isset($bankList[$card_5])) {
        return $bankList[$card_5];
    }
    $card_4 = substr($card, 0, 4);
    if (isset($bankList[$card_4])) {
        return $bankList[$card_4];
    }
    return false;

}